/***************************************************************************
 创建者: 华磊
 开始时间: 2018.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************
 *  @file x.h
 *  功能:
 *
 *                                                                         *
 ***************************************************************************/
#ifndef ROBOTENGINE_H
#define ROBOTENGINE_H

#ifndef D_ROBOT_INFO_FILE_NAME
#define D_ROBOT_INFO_FILE_NAME "robotInfo.xml"
#endif


#include <QThread>
#include <QVector>
#include <QObject>

#include "domparser.h"
#include "../MacroDefine/GeneralDefine.h"

#include "robotio.h"
#include "robotsettings.h"
#include "md5check.h"
#include "fleetdefine.h"

//class FleetService;
class ProgramEngine;
class McControllerThread;
/**
 * @brief The RobotEngine class 机器人引擎类
 */
#ifdef ROBOTENGINE_LIBRARY
#include "robotengine_global.h"
class    ROBOTENGINESHARED_EXPORT   RobotEngine : public QObject,  public DomParser
#else
class  RobotEngine : public QObject,  public DomParser
#endif
{
Q_OBJECT
public:
    RobotEngine(QObject *parent=0);
    ~RobotEngine();

public:
    int getWeldSystemInfo(struct WeldSystemInfo &weldSystemInfo);
    int changeWeldSystemInfo(struct WeldSystemInfo configIn);
    int getWeldCondition(std::vector<WeldCondition> &configListOut);
    int changeWeldConditionConfig(struct WeldCondition &weldCondition);
    int getWeldProperty(std::vector<WeldProperty> &configListOut);
    int changeWeldPropertConfig(struct WeldProperty &weldProperty);
    int getProgramSetRunFile(QString &fileName);
    int getEngineStatue();
    int setEngineStatus_fleet(STOP_COMMAND_TYPE statusIn);
    int addMonitorDi_fleet(bool isOr,QVector<int> diIndex,QVector<int> diValue);
    E_PROGRAM_LOOP_RESULT moveRobotArm_run(const QVector<int> &lineList,
                                   const QVector<RobotArmMoveParameter> &pathListParaIn, int isLastPointMove=0);
    E_PROGRAM_LOOP_RESULT moveMultiPoint_run(const QVector<int> &lineList,
                                   const QVector<LaserPathParameter> &laserPathListParaIn, int isLastPointMove=0);
    E_PROGRAM_LOOP_RESULT moveMagnetic_run(const MaganeticPathParameter paraIn);
    E_PROGRAM_LOOP_RESULT roller_run(const RollerParameter& paraIn);
    E_PROGRAM_LOOP_RESULT charging_run(const ChargeParameter& paraIn);
    E_PROGRAM_LOOP_RESULT liftMechanism_run(const LiftParameter& paraIn);
    int playSystemMusic(QString nameIn, int musicPlayTimes);
    int playMusic(QString musicFileName,int musicPlayTimes,int musicVolume);
    int playBarriorMusic(double barrierRatioIn);
    int playErrorMusic();
    int restoreNormalMusic();
    int clearAllMsg( );
    int addCommand(UserCommandData commandIn);
    int controllerShutDown();
    int setRunModel(EN_CONTROL_TYPE runModelIn);
    int getEmergencyDi(bool &emStop1, bool &emStop2);
    int getServoOnDi(bool &servoOnDi);
    unsigned short getDi_short(int shortIndex);//返回１６ｂｉｔｄｉ
    unsigned short getDo_short(int shortIndex);//返回１６ｂｉｔｄo
    int setDoVector(unsigned short doListIn, unsigned short doValidMask);
    bool getCustomDiStatus(int index);
    bool getSystemDiStatus(int index);
    bool getSystemDoStatus(int index);
    int getCustomDoStatus(int index);
    int setSystemDo(int index, bool val);
    int setCustomDo(int index, bool val);
    int setAo(int index, double valIn);
    int setCustomDi(int index, bool val);

    int readVersion(std::vector <ComponentVersion> &componentVersionList);


    int getRobotBasicStatus();

    /**
    * @brief readRobotInfoXml   ///读取机器人基本信息
    * @param xmlfile   路径+文件名
    * @return   true if ok
    */
   bool readRobotInfoXml(QString xmlfile);

   ///外部参数修改后，通知组件更新参数
   bool updateRobotConfig( );

   /**
    * @brief writeExternModelToFile
    * @param externModel
    * @return
    */
   int writeExternModelToFile(int externModel);
    /**
     * @brief readDefaultFile       获取缺省文件名称
     */
    bool readDefaultFileSetting();
    /******************************************************************************
     * 日期：2018年11月2日
     * 修改人：丁培
     * 修改内容: 添加写入默认程序
     * **************************************************************************/
    /**
     * @brief writeDefaultFileSetting       写入缺省文件名称
     */
    int writeDefaultFileSetting(std::string inFileName);

    /**
     * @brief robotServoStatus        获取当前机器人的伺服状态（伺服ｏｎ、伺服ｏｆｆ、伺服报警）
     */
    int robotServoStatus();

signals:
   void openFile_signal(QString strFilePath);
   void openDefaultFile_signal();
   void signal_runProgram( );
   void signal_ceaseProgram();
   void signal_resetProgram();
   void signal_releaseHoldOn();
   void signal_setErrorMsgModel();
   void signal_setStartLine(int startLine);
//   void signal_setTimerReg(int timerNum,int changeStatus);

public slots:
   /**
    * @brief init 初始化函数
    */
   void init(int startType);

   //运动状态，手动单步，手动连学
//    /**
//     * @brief timeOutDo 定时更新数据／ＩＯ
//     */
//    void timeOutDo();

    /**
     * @brief readProgram 打开/解释文件
     * @param strFilePath
     * @return
     */
    int  openFile( QString strFilePath );
    int  openDefaultFile(  );

    /**
      * @brief runProgram 运行文件
      */
     int runProgram( );
     /**
      * @brief ceaseProgram     暂停
      */
     int ceaseProgram();
     int quickStop();
     /**
      * @brief resetProgram     停止
      */
     int resetProgram();
     /**
      * @brief releaseHoldOn    松开使能按键
      */
     void releaseHoldOn();
     /**
      * @brief setErrorMsgModel     发生错误的处理逻辑
      */
     void setErrorMsgModel();
     /**
      * @brief setStartLine             设置从那一行开始运行
      * @param startLine
      */
     void setStartLine(int startLine);
//     /**
//      * @brief setTimerReg
//      * @param timerNum             时间寄存器号码
//      * @param changeStatus        状态
//      */
//     void setTimerReg(int timerNum,int changeStatus);

private:
     int readConfigFile(QString configFilePathIn);

public:


    /**
     * @brief robotInfo  ///机器人信息
     */
    RobotInfo robotInfo;
    //机器人内部组件
    /**
     * @brief ioController IO控制
     */
    RobotIo* ioController;
    /**
     * @brief programEngine　指令解释，逻辑处理
     */
    ProgramEngine* programEngine;
    //    FleetService* fleetService;

        //机器人内部线程
        /**
        * @brief threadProgramEngine //程序解释线程（主要组件的生存线程）
        */
    //    QThread  *threadProgramEngine;
        /**
         * @brief threadMotionEngine //算法线程
         */
        McControllerThread  *threadMotionEngine;

    /**
      * @brief defaultFileName       缺省文件名称
      */
     std::string defaultFileName;

     /**
      * @brief defaultFile           缺省文件
      */
     QString defaultFile;
     /**
      * @brief externModel
      */
     int externModel;

#if 0
    // QThread  *threadPeriod; //周期线程（安全监控，io更新）預留
    // McControllerInterpreter* mcControllerInterpreter; // 算法
    // InternalSafe* internalSafe;//安全
    GlobalRegister* globalRegister;
    GlobalFile *globalFile;
    GlobalCommand* globalCommand;
    GlobalSafe*  globalSafe ;
    MessageLog* messageLog;
    RobotManager* robotManager;
#endif

    //RobotSettings* settings;


private:
        ComponentVersion versionInfo;
        bool isManualLooseServo;

};

#endif // ROBOTENGINE_H
